package com.city.controller;

import com.city.domain.pub.PubUser;
import com.city.domain.report.CtBranch;
import com.city.domain.report.CtMessage;
import com.city.helper.AuthHelper;
import com.city.repository.pub.PubUserRepository;
import com.city.repository.report.CtBranchRepository;
import com.city.repository.report.CtReportRepository;
import com.city.repository.report.format.CtUserFoematRepository;
import com.city.service.report.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * @deprecation 智慧兴文公共信息平台-主页
 * @email leichuqiao@126.com
 * @author 雷楚桥
 * @date 2018-07-05 19:08
 */
@Controller
public class HomeController {

    @Autowired
    private MessageService messageS;
    @Autowired
    private AuthHelper authH;
    @Autowired
    private CtReportRepository ctReportRepository;
    @Autowired
    private CtBranchRepository ctBranchRepository;
    @Autowired
    private PubUserRepository userRepository;
    @Autowired
    private CtUserFoematRepository ctUserFoematRepository;

    /**
     * @deprecation 主页重定向
     * @author 雷楚桥
     * @date 2018-07-06 17:17
     */
    @RequestMapping(value = {"/home","/home.jsp","/home.htm","/home.do","/home.page"})
    public void index_response(HttpServletResponse response) throws IOException {
        response.sendRedirect("/home.html?system=1");
    }

    /**
     * @deprecation 主页
     * @author 雷楚桥
     * @date 2018-07-05 19:10
     */
    @RequestMapping("/home.html")
    public String index(@Param("system") Integer system,
                        Map<String,Object> map, HttpSession session){
        map.put("system",system);
        map.put("user",session.getAttribute("laowangUser"));
        map.put("titleName","智慧兴文城乡综合信息平台");

        return "/home/index";
    }

    /**
     * @deprecation 我的消息
     * @author 雷楚桥
     * @date 2018-09-12 14:19
     */
    @RequestMapping("/message.html")
    public String message_html(Map<String,Object> map){

        List<CtMessage> messages = messageS.findMessage(authH.thisUserId());

        for (int i=0;i<messages.size();i++){
            messages.get(i).setTask(ctReportRepository.findOne(messages.get(i).getTaskId()).getTaskId());
            messages.get(i).setSendName(ctUserFoematRepository.findByUserId(messages.get(i).getSendId()).getName());
            if(messages.get(i).getAcceptId() == 0){
                messages.get(i).setAcceptName("指挥中心");
            }else {
                messages.get(i).setAcceptName(ctBranchRepository.findOne(messages.get(i).getAcceptId()).getName());

            }
//            System.out.println(messages.get(i).getAcceptId());

        }


        map.put("messageList",messages);

        map.put("titleName","答复授权");
        return "/home/message";
    }
}
